package com.typhoon.spring_jdbctemplate.container;

import com.typhoon.spring_jdbctemplate.service.ReturnAddressService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义寻址容器
 */
@Component
public class CustomReturnAddressHolder implements ApplicationContextAware {

    private static Map<String,ReturnAddressService> map = new HashMap<>();

    /**
     * 初始化容器
     *
     * @param applicationContext
     * @throws BeansException
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(null != applicationContext) {
            Map<String,ReturnAddressService> beans = applicationContext.getBeansOfType(ReturnAddressService.class);
            if(null != beans && !beans.isEmpty()) {
                beans.forEach((k,v) -> {
                    map.put(v.getCode(),v);
                });
            }
        }
    }

    /**
     * 获取自定义执行器
     *
     * @param code
     * @return
     */
    public ReturnAddressService getByCode(String code) {
        return map.get(code);
    }
}
